package com.gz.service.impl;

import com.gz.entity.Answers;
import com.gz.dao.AnswersMapper;
import com.gz.service.AnswersService;
import com.gz.vo.R;
import org.springframework.stereotype.Service;
import com.gz.dto.AnswersDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gz.dto.PageData;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * 用户答题表(Answers)表服务实现类
 *
 * @author makejava
 * @since 2024-05-27 15:04:02
 */
@Service
public class AnswersServiceImpl implements AnswersService {
    @Autowired
    private AnswersMapper answersDao;

    /**
     * 通过ID查询单条数据
     *
     * @param answerId 主键
     * @return 实例对象
     */
    @Override
    public Answers queryById(Integer answerId) {
        return answersDao.queryById(answerId);
    }
    @Override
    public R<?> queryAnswerResById(Integer answerId){
        return R.ok(answersDao.queryAnswerResById(answerId));
    }

    /**
     * 分页查询
     *
     * @param dto 筛选条件
     * @return 查询结果
     */
    @Override
    public PageData queryByPage(AnswersDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<Answers> list = answersDao.queryAll(dto);
        PageInfo<Answers> pageInfo = new PageInfo<>(list);
        return new PageData<>(pageInfo.getTotal(), list);
    }

    /**
     * 条件查询
     *
     * @param dto 筛选条件
     * @return 查询结果
     */
    @Override
    public List<Answers> queryAll(AnswersDto dto) {
        List<Answers> list = answersDao.queryAll(dto);
        return list;
    }

    /**
     * 新增数据
     *
     * @param answers 实例对象
     * @return 实例对象
     */
    @Override
    public Answers insert(Answers answers) {
        answersDao.insertSelective(answers);
        return answers;
    }

    @Override
    public int insertBatch(List<Answers> list) {
        return answersDao.insertBatch(list);
    }

    /**
     * 修改数据
     *
     * @param answers 实例对象
     * @return 实例对象
     */
    @Override
    public Answers update(Answers answers) {
        answersDao.update(answers);
        return queryById(answers.getAnswerId());
    }

    /**
     * 通过主键删除数据
     *
     * @param answerId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer answerId) {
        return answersDao.deleteById(answerId) > 0;
    }
}
